package com.main.ok;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.main.common.bind.annotation.RequestJsonParam;
import com.main.common.util.MapWapper;
import com.main.vo.UserModel;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        String formattedDate = dateFormat.format(date);
        model.addAttribute("serverTime", formattedDate);
        return "home";
    }

    @RequestMapping("/file")
    public String file() {
        return "file";
    }

    @RequestMapping(value = "/test", headers = "name", method = RequestMethod.GET)
    public String test(String name, HttpServletRequest request) {
        System.out.println(name);
        System.out.println(request.getHeader("name"));
        System.out.println(request.getHeader("age"));
        System.out.println(request.getCharacterEncoding());
        return "helloworld";

    }

    @RequestMapping(value = "/testvoid")
    public void testvoid(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf8");
            response.getWriter().println("<font style='color:red'>hello</font>");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * InputStream OutputStream 也可以作为参数
     * 
     * @param reader
     * @param writer
     */
    @RequestMapping("/testrw")
    public void testrw(Reader reader, Writer writer) {
        try {
            writer.write("hello,ketty");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * http://localhost:8080/list?list=[1,2,34]
     * 
     * @param list
     * @return
     */
    @RequestMapping("/list")
    public @ResponseBody
    List<Integer> list(@RequestJsonParam("list") List<Integer> list) {
        for (Integer integer : list) {
            System.out.println(integer);
        }
        return list;
    }

    /**
     * http://localhost:8080?set=[{"username":"123"},{"username":"234"}]
     * 
     * @param set
     * @return
     */
    @RequestMapping("/set")
    public @ResponseBody
    Set<UserModel> map(@RequestJsonParam(value = "set") Set<UserModel> set) {
        for (UserModel userModel : set) {
            System.out.println(userModel.getUsername());
        }
        return set;
    }

    /**
     * http://localhost:8080/jsonlist?list=[{"username":"123"},{"username":"234"
     * }]
     * 
     * @param list
     * @return
     */
    @RequestMapping("/jsonlist")
    public @ResponseBody
    List<UserModel> jsonlist(@RequestJsonParam(value = "list") List<UserModel> list) {
        for (UserModel userModel : list) {
            System.out.println(userModel.getUsername());
        }
        return list;
    }

    /**
     * http://localhost:8080/map?map={"a":{"username":"123"},"b":{"username":
     * "234"}}
     * 
     * @param map
     * @return
     */
    @RequestMapping("/map")
    public @ResponseBody
    MapWapper<String, UserModel> map(@RequestJsonParam(value = "map") MapWapper<String, UserModel> map) {
        for (Entry<String, UserModel> entry : map.entrySet()) {
            System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
        }
        return map;
    }
}
